home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / lyrics / LyrcParser.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  3.0 KB  |  87 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
  2. #
  3. # Copyright (C) 2007 James Livingston
  4. # Copyright (C) 2007 Sirio Bola√±os Puchet
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  12. # GStreamer plugins to be used and distributed together with GStreamer
  13. # and Rhythmbox. This permission is above and beyond the permissions granted
  14. # by the GPL license by which Rhythmbox is covered. If you modify this code
  15. # you may extend this exception to your version of the code, but you are not
  16. # obligated to do so. If you do not wish to do so, delete this exception
  17. # statement from your version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program; if not, write to the Free Software
  26. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  27.  
  28. import urllib
  29. import re
  30. import rb
  31.  
  32. EXPS = ['\n', '\r', '<[iI][mM][gG][^>]*>', '<[aA][^>]*>[^<]*<\/[aA]>',
  33.     '<[sS][cC][rR][iI][pP][tT][^>]*>[^<]*(<!--[^>]*>)*[^<]*<\/[sS][cC][rR][iI][pP][tT]>',
  34.     '<[sS][tT][yY][lL][eE][^>]*>[^<]*(<!--[^>]*>)*[^<]*<\/[sS][tT][yY][lL][eE]>']
  35. CEXPS = [re.compile (exp) for exp in EXPS]
  36.  
  37. SEPARATOR_RE = re.compile("<[fF][oO][nN][tT][ ]*[sS][iI][zZ][eE][ ]*='2'[ ]*>")
  38.  
  39.  
  40. class LyrcParser (object):
  41.     def __init__(self, artist, title):
  42.         self.artist = artist
  43.         self.title = title
  44.     
  45.     def search(self, callback, *data):
  46.         path = 'http://www.lyrc.com.ar/en/'
  47.  
  48.         wartist = urllib.quote(self.artist)
  49.         wtitle = urllib.quote(self.title)
  50.         wurl = 'tema1en.php?artist=%s&songname=%s' % (wartist, wtitle)
  51.         
  52.         loader = rb.Loader()
  53.         loader.get_url (path + wurl, self.got_lyrics, callback, *data)
  54.  
  55.     def got_lyrics(self, lyrics, callback, *data):
  56.         if lyrics is None:
  57.             callback (None, *data)
  58.             return
  59.  
  60.         for exp in CEXPS:
  61.             lyrics = exp.sub('', lyrics)
  62.  
  63.         lyricIndex = SEPARATOR_RE.search(lyrics)
  64.  
  65.         if lyricIndex is not None:
  66.             callback(self.parse_lyrics(SEPARATOR_RE.split(lyrics, 1)[1]), *data)
  67.         else:
  68.             callback (None, *data)
  69.  
  70.     def parse_lyrics(self, lyrics):
  71.         if re.search('<p><hr', lyrics):
  72.             lyrics = re.split('<p><hr', lyrics, 1)[0]
  73.         else:
  74.             lyrics = re.split('<br><br>', lyrics, 1)[0]
  75.         
  76.         lyrics = re.sub('<[fF][oO][nN][tT][^>]*>', '', lyrics)
  77.         title = re.split('(<[bB]>)([^<]*)', lyrics)[2]
  78.         artist = re.split('(<[uU]>)([^<]*)', lyrics)[2]
  79.         lyrics = re.sub('<[bB]>[^<].*<\/[tT][aA][bB][lL][eE]>', '', lyrics)
  80.         lyrics = re.sub('<[Bb][Rr][^>]*>', '\n', lyrics)
  81.         titl = "%s - %s\n\n" % (artist, title)
  82.         lyrics = titl + lyrics
  83.         lyrics += "\n\nLyrics provided by lyrc.com.ar"
  84.  
  85.         return lyrics
  86.  
  87.